import {
  scrollMixin
} from 'utils/mixin';
import {
  Toast
} from 'vant';
import cTitle from "components/title";
export default {
  mixins: [scrollMixin], // 加载更多
  data() {
    return {
      agentLevel: '', //代理商等级
      username: '', //会员昵称
      avatar_image: '', //会员头像
      sale: [], //销售额(第一个为本月销售额，第二个为年度销售额)
      activeName: 1, //销售额选中
      slideDown: false, //下拉筛选是否显示
      selectedTime: this.dealdate(new Date()), //当前选中的筛选时间
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      salesDetail: [], //销售明细
      screenTime: [2022.04, 2022.05, 2022.06], //筛选时间
      orderLastPage:false,//是否为最后一页
      isLoadMore:false,//是否显示加载更多
      page:1,//时间列表当前页
      total_page:0,
    };
  },
  activated() {

  },
  mounted() {
    this.getData(this.selectedTime)
  },
  methods: {
    //跳转我的团队销售额
    goMy_team_sales() {
      this.$router.push(this.fun.getUrl("my_team_sales"));
    },
    changeSlideDown(value) { //弹出时间选择弹窗
      if (value) {
        this.selectedTime = this.dealdate(value)
        this.getData(this.selectedTime)
      }
      if (this.slideDown) {
        this.slideDown = false
      } else {
        this.slideDown = true
      }
    },
    cancelTimePopover() { //时间取消修改
      this.slideDown = false
    },
    formatter(type, val) {
      if (type === 'month') {
        return `${val}月`;
      } else if (type === 'day') {
        return `${val}日`;
      }
      return val;
    },
    dealdate(dealdate) { //时间转换
      const date = new Date(dealdate)
      const year = date.getFullYear()
      let month = date.getMonth() + 1
      month = month < 10 ? "0" + month : month
      let day = date.getDate()
      day = day < 10 ? "0" + day : day
      let hour = date.getHours()
      hour = hour < 10 ? "0" + hour : hour
      let minute = date.getMinutes()
      minute = minute < 10 ? "0" + minute : minute
      let second = date.getSeconds()
      second = second < 10 ? "0" + second : second
      return year + '-' + month;
    },
    screen(value) { //筛选
      this.selectedTime = value
    },
    //获取数据
    getData(month) {
      month = parseInt(month.replace('-', ''))
      this.page = 1
      $http.post('plugin.agent-earnings.frontend.sales-statistical.get-sales-list', {
        month_str: month,
        page:this.page
      }).then((res) => {
          if (res.result === 1) {
            let data = res.data
            this.isLoadMore = true;
            this.total_page = res.data.list.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.salesDetail = data.list?.data
            this.orderLastPage = false
            if(data.list.current_page>=data.list.last_page){ //判断订单列表是否为最后一页
              this.orderLastPage = true
            }
            this.agentLevel = data.agent_info.has_one_level.level_name
            this.username = data.agent_info.has_one_member.username
            this.avatar_image = data.agent_info.has_one_member.avatar_image
            this.sale[0] = data.current_month_sales
            this.sale[1] = data.annual_sales
          } else {
            this.$dialog.alert({
              message: res.msg
            });
          }
        },
        function(response) {
          // error callback
        });
    },
    getMoreData() {
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http.post('plugin.agent-earnings.frontend.sales-statistical.get-sales-list', {
          month_str: parseInt(that.selectedTime.replace('-', '')),
          page:that.page
        }, '加载中...').then(function (response) {
          that.isLoadMore = true;
          if (response.result === 1) {
            let data = response.data
            that.salesDetail = [...that.salesDetail,...data.list.data]//数组拼接
            that.orderLastPage = false //是否是最后一页
            console.log(data.list.current_page,data.list.last_page)
            if(data.list.current_page>=data.list.last_page){ //判断订单列表是否为最后一页
              that.orderLastPage = true
            }

          } else {
            that.page = that.page - 1;
            that.isLoadMore = false;
            return;
          }
        }, function (response) {
          // error callback
        });

      }
    },
  },
  components: {
    cTitle
  }
};
